package com.example.app1;

//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;
//import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
//import java.util.Random;



public class App1Activity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
	/** Called when the activity is first created. */
	Data dat;
    private int nYear;
    private int nMonth;
    private int nDay;
    private int nHour;
    private int nMinute;
    //private int random;
   // private Random rand;
    private EditText et1;
    private EditText et2;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		//rand = new Random();
		//random = rand.nextInt(100);
//fer que els botons esperin
        View Button = findViewById(R.id.data);
        Button.setOnClickListener(this);  
        View Button1 = findViewById(R.id.musica);
        Button1.setOnClickListener(this);  
    }
 //conectar els botons amb una funcio
    public void onClick(View v) {
    	switch (v.getId()) {
    		case R.id.data:
    			Intent i = new Intent(this, Data.class);
    			startActivity(i);
    			break;
    		case R.id.musica:
    			Intent j = new Intent(this, Musica.class);
    			startActivity(j);
    			break;
    			// More buttons go here (if any) ...
    		}
    	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menuhome, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.Save_all:
					//startActivity(new Intent(this, Prefs.class));
				//s'ha domplir amb lo que fa el save
					et1=(EditText)findViewById(R.id.textNota);
					et2=(EditText)findViewById(R.id.resume);
					nDay = Data.getDay();
					nMonth = Data.getMonth();
					nYear = Data.getYear();
					nHour = Data.getHour();
					nMinute = Data.getMinute();
					nDay = Data.getDay();
					//grabar();
					alta();
					finish();
					return true;
			case R.id.quit:
				//s/ha domplir amb lo k fa el cancel
				finish();
					return true;
	// More items go here (if any) ...
		}
		return false;
	}
	
	/*public void grabar() {
		
        //String nomarchivo = String.valueOf(random);
        String contenido= et1.getText().toString();
        String nomarchivo = et2.getText().toString();
        try
        {
            File tarjeta = Environment.getExternalStorageDirectory();
            File file = new File(tarjeta.getAbsolutePath(), nomarchivo);
            OutputStreamWriter osw =new OutputStreamWriter(new FileOutputStream(file));
            osw.write(contenido);
            osw.flush();
            osw.close();
            Toast.makeText(this,"Los datos fueron grabados correctamente",Toast.LENGTH_SHORT).show();
            et1.setText("");
            et2.setText("");
        }
        catch (IOException ioe){
        }
    }*/
	public void alta() {
        database admin=new database(this, "administracion", null, 1);
        SQLiteDatabase bd=admin.getWritableDatabase();
        String content=et1.getText().toString();
        String screen=et2.getText().toString();     
        ContentValues registro=new ContentValues();
        registro.put("screen",screen );
        registro.put("conntent",content );
        registro.put("dia",nDay );
        registro.put("mes",nMonth );
        registro.put("any",nYear );
        registro.put("hora",nHour );
        registro.put("min",nMinute );
        bd.insert("votantes", null, registro);
        bd.close();
        Toast.makeText(this, "the appointment have been saved", Toast.LENGTH_SHORT).show();        
    }
	/*public void modificacion(View v) {
        database admin=new database(this, "administracion", null, 1);
        SQLiteDatabase bd=admin.getWritableDatabase();
        String content=et1.getText().toString();
        String screen=et2.getText().toString();     
        ContentValues registro=new ContentValues();
        registro.put("screen",screen );
        registro.put("conntent",content );        
        int cant = bd.update("votantes", registro, "et2="+et2, null);
        bd.close();
        if (cant==1)
            Toast.makeText(this, "the appointment have been saved", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, "not saved", Toast.LENGTH_SHORT).show();        
    }*/
	
	//nHour = dat.getHour();
}

//http://www.htcmania.com/archive/index.php/f-153.html